package com.example.oms.service.impl;

import com.example.oms.dao.*;
import com.example.oms.vo.MedVo;
import com.example.oms.vo.PrescriptionAndMedListVo;
import com.example.oms.entity.*;
import com.example.oms.manager.DailyQueueManager;
import com.example.oms.service.DoctorService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class DoctorServiceImpl implements DoctorService {
    @Resource
    DoctorsDao doctorsDao;
    @Resource
    PatientsDao patientsDao;
    @Resource
    RegistrationsDao registrationsDao;
    @Resource
    DiagRecordsDao diagRecordsDao;
    @Resource
    PrescriptionsDao prescriptionsDao;
    @Resource
    private MedsDao medsDao;
    @Resource
    private DiagReportsDao diagReportsDao;
    @Resource
    private DailyQueueManager dailyQueueManager;

    @Override
    public List<Map<String, Object>> getAllPatientsByIdAndDate(int docId, Date date) {
        System.out.println(date);
        List<Map<String, Object>> tmpList = registrationsDao.getRegByDocIdAndDateJoinDoctorsJoinPatients(docId, date);
        List<Map<String, Object>> result = new ArrayList<>();
        for (Map<String, Object> tmp : tmpList) {
            Map<String, Object> newItem = new HashMap<>();
            Map<String, Object> regInfo = new HashMap<>();
            Map<String, Object> patientInfo = new HashMap<>();

            regInfo.put("id", tmp.get("id"));
            regInfo.put("doctor_id", tmp.get("doctor_id"));
            regInfo.put("doctor_name", tmp.get("doctor_name"));
            regInfo.put("status", tmp.get("status"));
            regInfo.put("appointment_date", tmp.get("appointment_date"));
            regInfo.put("appointment_period", tmp.get("appointment_period"));
            regInfo.put("is_appointment_period", tmp.get("is_appointment_period"));
            regInfo.put("time", tmp.get("time"));

            patientInfo.put("id", tmp.get("patient_id"));
            patientInfo.put("name", tmp.get("patient_name"));
            patientInfo.put("birthday", tmp.get("patient_birthday"));
            patientInfo.put("sex", tmp.get("patient_sex"));
            patientInfo.put("tel", tmp.get("patient_tel"));

            newItem.put("reg_info", regInfo);
            newItem.put("patient_info", patientInfo);
            result.add(newItem);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> getQueueListByDocId(int docId) {
        List<Map<String, Object>> result = new ArrayList<>();
        Map<String, LinkedList<Patients>> queues = dailyQueueManager.getQueues(docId);
        queues.forEach((k, v) -> {
            Map<String, Object> map = new HashMap<>();
            map.put("period", k);
            map.put("patients", v);
            result.add(map);
        });
        return result;
    }

    @Override
    public Patients getPatientDetailById(int id) {
        return patientsDao.queryById(id);
    }

    @Override
    public List<Registrations> getRegistrationDetailByDocIdAndDate(int docId, Date date) {
        Registrations queryCondition = new Registrations();
        queryCondition.setDoctorId(docId);
        queryCondition.setAppointmentDate(date);

        // 先不考虑分页问题
        Pageable pageable = PageRequest.of(0, 999);
        return registrationsDao.queryAllByLimit(queryCondition, pageable);
    }

    @Override
    public List<Patients> getPatientsByIds(List<Integer> ids) {
        return patientsDao.queryByIds(ids);
    }


    @Override
    public void updateDiagRecord(DiagRecords diagRecord) {
        diagRecordsDao.updateDiagRecord(diagRecord);
    }

    @Override
    public List<PrescriptionAndMedListVo> getPrescriptionByDiagRecordId(int diagRecordId) {
        List<Prescriptions> prescriptions = prescriptionsDao.getPrescriptionsByDiagRecordId(diagRecordId);

        List<PrescriptionAndMedListVo> result = new ArrayList<>();
        for (Prescriptions prescription : prescriptions) {
            List<MedVo> medList = medsDao.getMedListByPresId(prescription.getId());
            PrescriptionAndMedListVo prescriptionAndMedListVo = new PrescriptionAndMedListVo();
            prescriptionAndMedListVo.setPres(prescription);
            prescriptionAndMedListVo.setMedsList(medList);
            result.add(prescriptionAndMedListVo);
        }
        return result;
    }

    @Override
    public List<PrescriptionAndMedListVo> getPresListByPatientId(int patientId, int pageNum) {
        Pageable pageable = PageRequest.of(pageNum, 10);
        List<Prescriptions> prescriptions = prescriptionsDao.getPrescriptionsByPatientId(patientId, pageable);

        List<PrescriptionAndMedListVo> result = new ArrayList<>();
        for (Prescriptions prescription : prescriptions) {
            List<MedVo> medList = medsDao.getMedListByPresId(prescription.getId());
            PrescriptionAndMedListVo prescriptionAndMedListVo = new PrescriptionAndMedListVo();
            prescriptionAndMedListVo.setPres(prescription);
            prescriptionAndMedListVo.setMedsList(medList);
            result.add(prescriptionAndMedListVo);
        }
        return result;

    }

    @Override
    public PrescriptionAndMedListVo getPrescriptionByPresId(int presId) {
        Prescriptions prescription = prescriptionsDao.getPrescriptionsById(presId);

        List<MedVo> medList = medsDao.getMedListByPresId(prescription.getId());
        PrescriptionAndMedListVo prescriptionAndMedListVo = new PrescriptionAndMedListVo();
        prescriptionAndMedListVo.setPres(prescription);
        prescriptionAndMedListVo.setMedsList(medList);
        return prescriptionAndMedListVo;
    }


    @Override
    public void newPrescription(PrescriptionAndMedListVo prescriptionAndMedList) {
        Prescriptions prescription = new Prescriptions();
        prescription.setDoctorId(prescriptionAndMedList.getDoctorId());
        prescription.setPatientId(prescriptionAndMedList.getPatientId());
        prescription.setStatus(prescriptionAndMedList.getStatus());
        prescription.setTime(prescriptionAndMedList.getTime());
        prescription.setRemarks(prescriptionAndMedList.getRemarks());
        prescription.setDiagRecordId(prescriptionAndMedList.getDiagRecordId());
        int newPresId = prescriptionsDao.newPrescription(prescription);

        List<MedVo> medList = prescriptionAndMedList.getMedsList();
        if (medList != null && medList.size() > 0) {
            medsDao.insertMedList(medList, prescription.getId());
        }

    }

    @Override
    public void newDiag(DiagReports diagReports) {
        diagReportsDao.newDiagReport(diagReports);
    }

    @Override
    public List<DiagReports> getDiagReportsByDiagRecordId(int diagRecordId) {
        return diagReportsDao.getDiagReportsByDiagRecordId(diagRecordId);
    }

    @Override
    public List<Meds> getAllMeds() {
        return medsDao.getAllDrugs();
    }

    @Override
    public int updateRegStatusByRegId(int regId, String status) {
        Registrations registrations = new Registrations();
        registrations.setId(regId);
        registrations.setStatus(status);
        registrationsDao.update(registrations);
        return 0;
    }


    @Override
    public DiagRecords getRecordByRegId(int regId) {
        return diagRecordsDao.getDiagRecordsByRegId(regId);
    }

    @Override
    public int newDiagRecord(DiagRecords diagRecords) {
        diagRecordsDao.newDiagRecord(diagRecords);
        return diagRecords.getId();
    }

    @Override
    public void popQueue(int docId,String period){
        dailyQueueManager.confirm(docId,period);
    }

    @Override
    public Registrations getRegByRegId(int regId){
        return registrationsDao.queryById(regId);
    }
}
